package com.training.java.redbox;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.training.java.redbox.domain.Customer;
import com.training.java.redbox.domain.CustomerDAO;
import com.training.java.redbox.domain.CustomerDaoJdbcImpl;
import com.training.java.redbox.domain.DAOException;

public class SignUp extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SignUp() {
		super();
	}

		protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		RequestDispatcher rd = request
				.getRequestDispatcher("/WEB-INF/SignUp.jsp");
		rd.forward(request, response);
	}

	
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerDAO customerDao = new CustomerDaoJdbcImpl();

		//long customerId = Long.parseLong(request.getParameter("customerId"));
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String address = request.getParameter("address");
		String city = request.getParameter("city");
		String state = request.getParameter("state");
		String zipCode = request.getParameter("zipcode");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		Customer customer = new Customer(0, firstName, lastName);
		customer.setAddress(address);
		customer.setCity(city);
		customer.setState(state);
		customer.setZipCode(zipCode);
		customer.setUserName(username);
		customer.setPassword(password);

		try {
			customerDao.addCustomer(customer);

			request.setAttribute("AckMessage", "Your Account has been created. You can login here now.");
			RequestDispatcher rd = request
					.getRequestDispatcher("/WEB-INF/LogIn.jsp");
			rd.forward(request, response);
			
			
			//normally response.redirect

		} catch (DAOException e) {
			e.printStackTrace();
		}
	}

}
